package cn.edu.nuc.zyb.server;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

import cn.edu.nuc.zyb.model.Cart;
import cn.edu.nuc.zyb.model.Goods;
import cn.edu.nuc.zyb.model.User;

public class Server extends Thread {

	public ServerSocket socket;

	public Server() {
		try {
			socket = new ServerSocket(4000);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// 发送boolean值来校验数据
	public void send(boolean flag, Socket socket) {
		OutputStream os = null;
		DataOutputStream dos = null;
		try {
			os = socket.getOutputStream();
			dos = new DataOutputStream(os);
			dos.writeBoolean(flag);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Override
	public void run() {
		while (true) {
			Socket s = null;
			InputStream is = null;
			DataInputStream dis = null;
			PrintWriter writer = null;
			try {
				s = socket.accept();
				is = s.getInputStream();
				dis = new DataInputStream(is);
				String msg = dis.readUTF();
				String m[] = msg.split(",");
				switch (m[0]) {
				case "login":
					File file = new File("D:\\store\\user");
					File[] files = file.listFiles();
					int sum = 0;
					FileInputStream fis = null;
					ObjectInputStream ois = null;
					for (int i = 0; i < files.length; i++) {
						fis = new FileInputStream(files[i]);
						ois = new ObjectInputStream(fis);
						User user = (User) ois.readObject();
						int password = Integer.valueOf(m[2]);

						if (m[1].equals(user.getUsername()) && password == user.getPassword()) {
							boolean flag = true;
							send(flag, s);
							System.out.println("登陆成功" + flag);
						} else {
							sum++;
							/*
							 * boolean flag = false; send(flag, s);
							 * System.out.println("登陆失败" + flag);
							 */
						}
						fis.close();
						ois.close();
					}
					if (sum == files.length) {
						boolean flag = false;
						send(flag, s);
						System.out.println("登陆失败" + flag);
					}
					break;
				case "regist":
					File file2 = new File("D:\\store\\user\\" + m[1]);
					if (file2.exists()) {
						boolean flag = false;
						send(flag, s);
						System.out.println("注册失败");
					} else {
						FileOutputStream fos = new FileOutputStream(file2);
						ObjectOutputStream oos = new ObjectOutputStream(fos);
						int password = Integer.valueOf(m[2]);
						int age = Integer.valueOf(m[4]);
						User user = new User(m[1], password, m[3], age);
						oos.writeObject(user);
						fos.close();
						oos.close();
						boolean flag = true;
						send(flag, s);
					}
					///
					break;
				case "addgoods":
					File file3 = new File("D:\\store\\goods");
					File[] files3 = file3.listFiles();
					int gid = files3.length;
					float price = Float.valueOf(m[2]);
					int num = Integer.valueOf(m[3]);

					FileOutputStream fos = new FileOutputStream("D:\\store\\goods\\" + m[1]);
					ObjectOutputStream oos = new ObjectOutputStream(fos);
					Goods goods = new Goods(gid, m[1], price, num, m[4]);

					oos.writeObject(goods);
					fos.close();
					oos.close();
					// 我这 直接添加成功 没有进行判断
					boolean flag = true;
					System.out.println("添加成功");
					send(flag, s);
					break;

				case "searchgoods":
					File file4 = new File("D:\\store\\goods");
					File[] files4 = file4.listFiles();
					ObjectOutputStream oo = new ObjectOutputStream(s.getOutputStream());
					List<Goods> list = new ArrayList<>();
					System.out.println(m[1]);
					FileInputStream fis1 = null;
					ObjectInputStream ois1 = null;
					for (int i = 0; i < files4.length; i++) {
						fis1 = new FileInputStream(files4[i]);
						ois1 = new ObjectInputStream(fis1);
						Goods goods1 = (Goods) ois1.readObject();
						String goodsName = m[1];
						if (goodsName.equals(goods1.getGoodsName())) {
							list.add(goods1);
							System.out.println("添加");
						}
					}
					fis1.close();
					ois1.close();
					System.out.println(list.size());
					oo.writeObject(list);

					break;
				case "listgoods":
					File file5 = new File("D:\\store\\goods");
					File[] files5 = file5.listFiles();
					ObjectOutputStream ooo = new ObjectOutputStream(s.getOutputStream());
					List<Goods> listgoods = new ArrayList<>();
					FileInputStream fis2 = null;
					ObjectInputStream ois2 = null;
					for (int i = 0; i < files5.length; i++) {
						fis2 = new FileInputStream(files5[i]);
						ois2 = new ObjectInputStream(fis2);
						Goods goods1 = (Goods) ois2.readObject();
						listgoods.add(goods1);

					}
					System.out.println(listgoods.size());
					fis2.close();
					ois2.close();
					ooo.writeObject(listgoods);

					break;
				case "adminsearchgoods":
					File file6 = new File("D:\\store\\goods");
					File[] files6 = file6.listFiles();
					ObjectOutputStream oooo = new ObjectOutputStream(s.getOutputStream());
					List<Goods> adminlistGoods = new ArrayList<>();
					System.out.println(m[1]);
					FileInputStream fis3=null;
					ObjectInputStream ois3=null;
					
					for (int i = 0; i < files6.length; i++) {
						 fis3 = new FileInputStream(files6[i]);
						 ois3 = new ObjectInputStream(fis3);
						Goods goods1 = (Goods) ois3.readObject();
						int id = Integer.valueOf(m[1]);
						if (id == goods1.getGid()) {
							adminlistGoods.add(goods1);
							System.out.println("添加");
						}
					}
					fis3.close();
					ois3.close();				
					System.out.println(adminlistGoods.size());
					oooo.writeObject(adminlistGoods);
					break;
				case "changegoods":
					File file7 = new File("D:\\store\\goods\\" + m[1]);
					if (file7.exists()) {
						FileInputStream fis4 = new FileInputStream("D:\\store\\goods\\" + m[1]);
						ObjectInputStream obi = new ObjectInputStream(fis4);
						Goods go = (Goods) obi.readObject();// 先从文件中读出来 反序列化出来
						fis4.close();
						obi.close();
						float prices = Float.valueOf(m[2]);
						int nums = Integer.valueOf(m[3]);
						Goods g = new Goods(go.getGid(), m[1], prices, nums, m[4]);

						FileOutputStream foss = new FileOutputStream("D:\\store\\goods\\" + m[1]);
						ObjectOutputStream ob = new ObjectOutputStream(foss);
						Goods gg = new Goods(go.getGid(), m[1], prices, nums, m[4]);
						ob.writeObject(gg);
						foss.close();
						ob.close();
						boolean flagg = true;
						send(flagg, s);
					} else {
						boolean flagg = false;
						send(flagg, s);
					}
					break;
				case "deletegoods":
					File file8 = new File("D:\\store\\goods\\" + m[1]);
					// if (file8.exists()) {
					boolean flags = file8.delete();
					send(flags, s);// 从服务器端返回消息来确定是否删除成功
					/*
					 * } else { boolean flags = false; send(flags, s); }
					 */
					break;
				case "lookgoods":
					FileInputStream fis6 = new FileInputStream("D:\\store\\goods\\" + m[1]);
					ObjectInputStream ois6 = new ObjectInputStream(fis6);
					Goods goods6 = (Goods) ois6.readObject();
					fis6.close();
					ois6.close();
					ObjectOutputStream oos6 = new ObjectOutputStream(s.getOutputStream());
					oos6.writeObject(goods6);
					break;
				case "addcart":

					File fie7 = new File("D:\\store\\cart\\" + m[5]);
					fie7.mkdir();
					FileOutputStream fos7 = new FileOutputStream(fie7 + "\\" + m[2]);
					ObjectOutputStream oos7 = new ObjectOutputStream(fos7);
					int id = Integer.valueOf(m[1]);
					float pri = Float.valueOf(m[3]);
					int numbers = Integer.valueOf(m[4]);
					Cart cart = new Cart(id, m[2], pri, numbers);
					oos7.writeObject(cart);
					oos7.close();
					boolean f = true;
					send(f, s);
					break;
				case "listcart":
					File fil8 = new File("D:\\store\\cart\\" + m[1]);
					List<Cart> l = new ArrayList<>();
					File[] fils8 = fil8.listFiles();
					FileInputStream fi8 = null;
					ObjectInputStream oi8 = null;
					for (int i = 0; i < fils8.length; i++) {
						fi8 = new FileInputStream(fils8[i]);
						oi8 = new ObjectInputStream(fi8);
						Cart c = (Cart) oi8.readObject();
						l.add(c);
					}
					fi8.close();
					oi8.close();
					ObjectOutputStream oos8 = new ObjectOutputStream(s.getOutputStream());
					oos8.writeObject(l);
					break;
				case "settleaccounts":
					File file9 = new File("D:\\store\\money\\"+m[1]);
					file9.mkdir();
					FileOutputStream fos8 = new FileOutputStream(file9 + "\\" + m[2]);
					ObjectOutputStream ooss8 = new ObjectOutputStream(fos8);
					ooss8.writeObject(m[2]);
					boolean ff = true;
					send(ff,s);
					break;
				}

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	}

}
